
using System;
using SharpNote.Core.Data;
using SharpNote.Core.Security;

namespace SharpNote.Core.Entity
{
	[TableInfoAttribute("NoteInfo")]
	public class NoteInfo : DbMappingObjectBase, IDecryptable
	{
		#region

		private System.String _id = Guid.NewGuid ().ToString ();

		[FieldMapping(true, System.Data.DbType.String, false, false)]
		public System.String Id {
			get { return _id; }
			set { _id = value; }
		}

		private System.String _title;

		[FieldMapping(false, System.Data.DbType.String, false, false)]
		public System.String Title {
			get { return _title; }
			set { _title = value; }
		}

		private System.String _content;

		[FieldMapping(false, System.Data.DbType.String, false, false)]
		public System.String Content {
			get { return _content; }
			set { _content = value; }
		}

		private System.String _tags;

		[FieldMapping(false, System.Data.DbType.String, false, false)]
		public System.String Tags {
			get { return _tags; }
			set { _tags = value; }
		}

		private System.DateTime _createTime = DateTime.Now;

		[FieldMapping(false, System.Data.DbType.DateTime, false, false)]
		public System.DateTime CreateTime {
			get { return _createTime; }
			set { _createTime = value; }
		}

		private System.Int64 _status = 1;

		[FieldMapping(false, System.Data.DbType.Int64, false, false)]
		public System.Int64 Status {
			get { return _status; }
			set { _status = value; }
		}

		private System.Byte[] _password = new byte[0];

		[FieldMapping(false, System.Data.DbType.Binary, false, false)]
		public System.Byte[] Password {
			get { return _password; }
			set { _password = value; }
		}



		#endregion

		#region Constructors

		public NoteInfo ()
		{
		}


		#endregion
		
		#region Methods
		
		public override string ToString ()
		{
		    return string.Format("[Id] \n{0}\n[Title] \n{1}\n[Content] \n{2}\n[Tags]\n{3}\n[Create time] \n{4}\n",
			                     (_id != null ? _id: string.Empty),
			                     (_title != null ? _title: string.Empty),
			                     (_content != null ? _content: string.Empty),
			                     (_tags != null ? _tags: string.Empty),
			                     _createTime.ToString());
		}
		
		#endregion

		#region IEncryptable implementation

		public bool Encrypt (string key)
		{
			if(key == null){ return false; }
			
			return StringEncryptionHelper.EncryptString(_title, key,
			                                              out _title)
				&& StringEncryptionHelper.EncryptString(_content, key,
				                                        out _content);
			                                              
		}

		public bool Decrypt (string key)
		{
			if(key == null){ return false; }
			
			return StringEncryptionHelper.DecryptString(_title, key,
			                                            out _title)
				&& StringEncryptionHelper.DecryptString(_content, key,
				                                        out _content);
		}

		#endregion

	}


}

